package com.bmadmin.controller.zcgl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SjkcxDomain;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.domain.TysxDomain;
import com.bmadmin.domain.ZcFlwhDomain;
import com.bmadmin.domain.ZcsqDomain;
import com.bmadmin.domain.ZcsqReceiverDomain;
import com.bmadmin.domain.ZcsqmxDomain;
import com.bmadmin.domain.ZcwhDomain;
import com.bmadmin.service.ZcFlwhService;
import com.bmadmin.service.ZcckService;
import com.bmadmin.service.ZcrkService;
import com.bmadmin.service.ZcsqService;
import com.bmadmin.service.ZcwhService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/zcsq")
public class ZcsqCountroller {
	private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Autowired
    ZcsqService zcsqService;
    @Autowired
    ZcFlwhService zcflwhService;
    @Autowired
    ZcrkService zcrkService;
    /**
     * 获取资产申请单列表 0--未提交 1--提交未审核 2-初审通过 3--初审未通过 4--处理人退回 5--处理人已接收 6--已处理 7--复审通过 8--复审未通过
     */
	@GetMapping("/getZcsqList")
    public Object getZcsqList(HttpSession session,ZcsqDomain zcsqDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", zcsqDomain.getQs_date());
		map.put("jz_date", zcsqDomain.getJz_date());
		if(zcsqDomain.getZ_state()==null){
			map.put("z_state", "0,3,4,8");//默认显示表单状态 0--未提交 3--审核未通过 5--接收人退回 8--复审未通过
		}else if("".equals(zcsqDomain.getZ_state())){
			map.put("z_state", "0,1,2,3,4,5,6,7,8");
		}else{
			map.put("z_state", zcsqDomain.getZ_state());
		}
		map.put("z_apply_userid", user.getU_id());
		logger.info(zcsqDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<ZcsqDomain> list = zcsqService.getZcsqList(map);
		
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 获取申请单明细列表
     */
    @GetMapping("/getZcsqmxList")
    public LayUiDataTablePOJO getZcsqmxList(String z_id) {
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();
        List<ZcsqmxDomain> list = zcsqService.getZcsqmxList(z_id);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setData(list);
        result.setCount(list.size());
        logger.info(result);
        return result;
    }
    
    /**
     * 根据资产分类获取接收人下拉列表
     */
    @GetMapping("/getClUserList")
    public NormalResult getClUserList(HttpSession session,String zcfl_gly){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
    	map.put("u_id", zcfl_gly);
    	List<ZcsqReceiverDomain> Users = zcsqService.getClUserList(map);
        NormalResult result = new NormalResult();
        result.setCode("0000");
        result.setMsg("success");
        result.setData(Users);
        return result;
    }
    
    /**
     * 获取接收人信息
     */
    @GetMapping("/getReceiveInfo")
    public NormalResult getReceiveInfo(HttpSession session,String z_receive_userid){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
    	map.put("u_id", z_receive_userid);
    	List<ZcsqReceiverDomain> ZcsqReceivers = zcsqService.getClUserList(map);
    	ZcsqReceiverDomain ZcsqReceiver = (ZcsqReceiverDomain)ZcsqReceivers.get(0);
        NormalResult result = new NormalResult();
        result.setCode("0000");
        result.setMsg("success");
        result.setData(ZcsqReceiver);
        return result;
    }
    
    @PostMapping("/deleteZcsq")
    public NormalResult deleteZcsq(String z_id) {
    	String log = "资产申请单删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(z_id)) {
            result.setCode("9999");
            result.setMsg("传入申请单ID为空!");
            return result;
        }
        int mxResult = zcsqService.deleteZcmxByid(z_id);
        int retResult = zcsqService.deleteZcsq(z_id);
        if(retResult>0&&mxResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/deleteZcmx")
    public NormalResult deleteZcmx(String z_mxid) {
    	String log = "资产明细删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(z_mxid)) {
            result.setCode("9999");
            result.setMsg("传入明细ID为空!");
            return result;
        }

        int retResult = zcsqService.deleteZcmx(z_mxid);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/upZcsq")
    public NormalResult upZcsq(String z_id) {
    	String log = "资产申请单提交>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(z_id)) {
            result.setCode("9999");
            result.setMsg("传入申请单ID为空!");
            return result;
        }

        int retResult = zcsqService.upZcsq(z_id);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("提交成功！");
            logger.info(log+"提交成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("提交失败！");
            logger.info(log+"提交失败！");
    	}
    	return result;
    }
	
    @PostMapping("/insertZcsq")
    public NormalResult insertZcsq(HttpSession session,String z_id,String zcfl_dm,String zc_bm,String zc_mc,String z_sl,
    		String z_apply_phone,String z_duetime,String z_receive_userid) {
    	String log = "资产领用新增>>>>>>>>>>>";
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	ZcsqDomain zcsqDomain = new ZcsqDomain();
        	if(!BMUtil.isNotNull(z_id)){
        		Map map = new HashMap();
            	map.put("zcfl_dm",zcfl_dm);
            	
//            	List<ZcFlwhDomain> list = zcflwhService.getZcflList(map);
//            	ZcFlwhDomain zcflwhDomain = list.get(0);
            	
        		zcsqDomain.setZcfl_dm(zcfl_dm);
        		zcsqDomain.setZ_apply_phone(z_apply_phone);
        		zcsqDomain.setZ_receive_userid(z_receive_userid);
        		zcsqDomain.setZ_apply_depid(user.getD_id());
        		zcsqDomain.setZ_apply_userid(user.getU_id());
        		zcsqDomain.setZ_apply_date(BMUtil.getNowTimeForString());
        		zcsqDomain.setZ_duetime(z_duetime);
        		if(zcsqService.insertZcsq(zcsqDomain)>0){
        			z_id = zcsqDomain.getZ_id();
        		}
        	}
        	ZcsqmxDomain zcsqmxDomain = new ZcsqmxDomain();
        	zcsqmxDomain.setZ_id(z_id);
        	zcsqmxDomain.setZc_bm(zc_bm);
        	zcsqmxDomain.setZc_mc(zc_mc);
        	zcsqmxDomain.setZ_sl(z_sl);
        	int retResult = zcsqService.insertZcsqmx(zcsqmxDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                result.setData(zcsqmxDomain);
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("资产申请单保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取资产申请单审批列表 0--未提交 1--提交未审核 2-初审通过 3--初审未通过 4--处理人退回 5--处理人已接收 6--已处理 7--复审通过 8--复审未通过
     */
	@GetMapping("/getZcspList")
    public Object getZcspList(HttpSession session,ZcsqDomain zcsqDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", zcsqDomain.getQs_date());
		map.put("jz_date", zcsqDomain.getJz_date());
		if(zcsqDomain.getZ_state()==null){
			map.put("z_state", "1");//默认显示表单状态 1--未审核
		}else if("".equals(zcsqDomain.getZ_state())){
			map.put("z_state", "1,2,3,4,5,6,7,8");
		}else{
			map.put("z_state", zcsqDomain.getZ_state());
		}
		map.put("z_apply_depid", user.getD_id());
		logger.info(zcsqDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<ZcsqDomain> list = zcsqService.getZcspList(map);
		
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 资产申请单审核
     */
    @PostMapping("/verifyZcsq")
    public NormalResult verifyZcsq(HttpSession session,ZcsqDomain zcsqDomain) {
    	String log = "资产申请单审核>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcsqDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_verify_view())){
        		result.setCode("9999");
                result.setMsg("部门意见为空！");
                logger.info(log+"部门意见为空！");
                return result;
        	}
        	
        	zcsqDomain.setZ_verify_userid(user.getU_id());
        	zcsqDomain.setZ_verify_date(BMUtil.getNowTimeForString());
        	int retResult = zcsqService.verifyZcsq(zcsqDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("审核成功！");
                logger.info(log+"审核成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("审核失败！");
                logger.info(log+"审核失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单审核异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取资产申请单复审列表 0--未提交 1--提交未审核 2-初审通过 3--初审未通过 4--处理人退回 5--处理人已接收 6--已处理 7--复审通过 8--复审未通过
     */
	@GetMapping("/getZcfsList")
    public Object getZcfsList(HttpSession session,ZcsqDomain zcsqDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", zcsqDomain.getQs_date());
		map.put("jz_date", zcsqDomain.getJz_date());
		if(zcsqDomain.getZ_state()==null){
			map.put("z_state", "2");//默认显示表单状态 1--未审核
		}else if("".equals(zcsqDomain.getZ_state())){
			map.put("z_state", "2,4,5,6,7,8");
		}else{
			map.put("z_state", zcsqDomain.getZ_state());
		}
		map.put("z_receive_depid", user.getD_id());
		logger.info(zcsqDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<ZcsqDomain> list = zcsqService.getZcfsList(map);
		
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 资产申请单复审
     */
    @PostMapping("/spZcsq")
    public NormalResult spZcsq(HttpSession session,ZcsqDomain zcsqDomain) {
    	String log = "资产申请单复审>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcsqDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_sp_view())){
        		result.setCode("9999");
                result.setMsg("处理部门意见为空！");
                logger.info(log+"处理部门意见为空！");
                return result;
        	}
        	
        	zcsqDomain.setZ_sp_userid(user.getU_id());
        	zcsqDomain.setZ_sp_date(BMUtil.getNowTimeForString());
        	int retResult = zcsqService.spZcsq(zcsqDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("审核成功！");
                logger.info(log+"审核成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("审核失败！");
                logger.info(log+"审核失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单审核异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取资产申请单处理列表 0--未提交 1--提交未审核 2-初审通过 3--初审未通过 4--处理人退回 5--处理人已接收 6--已处理 7--复审通过 8--复审未通过
     */
	@GetMapping("/getZcclList")
    public Object getZcclList(HttpSession session,ZcsqDomain zcsqDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", zcsqDomain.getQs_date());
		map.put("jz_date", zcsqDomain.getJz_date());
		if(zcsqDomain.getZ_state()==null){
			map.put("z_state", "1,2,4,5,7");//默认显示表单状态 2--审核通过
		}else if("".equals(zcsqDomain.getZ_state())){
			map.put("z_state", "1,2,4,5,6,7");
		}else{
			map.put("z_state", zcsqDomain.getZ_state());
		}
		map.put("z_receive_userid", user.getU_id());
		logger.info(zcsqDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<ZcsqDomain> list = zcsqService.getZcclList(map);
		
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 申请单接收
     */
    @PostMapping("/receiveZcsq")
    public NormalResult receiveZcsq(HttpSession session,ZcsqDomain zcsqDomain) {
    	String log = "资产申请单接收>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcsqDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	zcsqDomain.setZ_receive_date(BMUtil.getNowTimeForString());
        	int retResult = zcsqService.receiveZcsq(zcsqDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("接收成功！");
                logger.info(log+"接收成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("接收失败！");
                logger.info(log+"接收失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单接收异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单退回
     */
    @PostMapping("/backZcsq")
    public NormalResult backZcsq(HttpSession session,ZcsqDomain zcsqDomain) {
    	String log = "资产申请单退回>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcsqDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	zcsqDomain.setZ_receive_date(BMUtil.getNowTimeForString());
        	int retResult = zcsqService.receiveZcsq(zcsqDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("退回成功！");
                logger.info(log+"退回成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("退回失败！");
                logger.info(log+"退回失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单接收异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单处理
     */
    @PostMapping("/disposeZcsq")
    public NormalResult disposeZcsq(HttpSession session,ZcsqDomain zcsqDomain) {
    	String log = "资产申请单处理>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcsqDomain+"】");
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcsqDomain.getZ_back_result())){
        		result.setCode("9999");
                result.setMsg("处理结果为空！");
                logger.info(log+"处理结果为空！");
                return result;
        	}
        	
        	List<ZcsqmxDomain> list = zcsqService.getZcsqmxList(zcsqDomain.getZ_id());
        	if(list==null||list.size()==0){
        		result.setCode("9999");
                result.setMsg("未维护申请资产明细！");
                logger.info(log+"未维护申请资产明细！");
                return result;
        	}else{
        		for(int i=0;i<list.size();i++){
        			ZcsqmxDomain map = (ZcsqmxDomain)list.get(i);
        			int zc_sl = Integer.parseInt(map.getZc_sl());
        			int z_sl = Integer.parseInt(map.getZ_sl());
        			if(z_sl>zc_sl){
        				result.setCode("9999");
                        result.setMsg("资产编码【"+map.getZc_bm()+"】-资产名称【"+map.getZc_mc()+"】库存不足，无法完成出库！");
                        logger.info(log+"资产编码【"+map.getZc_bm()+"】-资产名称【"+map.getZc_mc()+"】库存不足，无法完成出库！");
                        return result;
        			}
        		}
        	}
        	
        	zcsqDomain.setZ_back_date(BMUtil.getNowTimeForString());
        	int retResult = zcsqService.disposeZcsq(zcsqDomain);
        	if(retResult>0){
        		for(int i=0;i<list.size();i++){
        			ZcsqmxDomain map = (ZcsqmxDomain)list.get(i);
        			int ret = zcrkService.DelZcSl(map.getZc_bm(),map.getZ_sl());
            		if(ret>0){
            			logger.info(log+"出库成功-资产编码【"+map.getZc_bm()+"】-资产名称【"+map.getZc_mc()+"】-出库数量【"+map.getZ_sl()+"】！");
            		}else{
            			logger.info(log+"出库库存修改失败！");
            		}
        		}
        		result.setCode("0000");
                result.setMsg("处理成功！");
                logger.info(log+"处理成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("处理失败！");
                logger.info(log+"处理失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单处理异常！");
            logger.error(e);
        }
        return result;
    }
}
